package game.core 
{
	import game.core.phases.*;
	import game.GameWorld;
	import game.objects.Building;
	
	/**
	 * ...
	 * @author Sulus Ltd.
	 */
	public class GameManager
	{
		public var world:GameWorld;
		private var turnPhases:Array;
		public var currentPlayer:Player;
		public var currentPlayerIdx:int = 0;
		private var currentPhase:TurnPhase;
		private var currentPhaseIdx:int = -1;
		private var turnFinished:Boolean = false;
		private var gameOver:Boolean = false;
		
		public var wind:Number = 0;
		public var gravity:Number = 9.0/30.0;
		
		public var players:Array;
		
		public var buildings:Array;
		
		public function GameManager() 
		{
			
		}
		
		
		public function Init(world:GameWorld,players:Array):void
		{
			this.world = world;
			this.players = players;
			
			buildings = [];
		}
		
		public function Die():void
		{
			if (currentPhase)
			{
				currentPhase.Finish();
			}			
		}
		
		public function Update():void
		{
			if (currentPhase)
				currentPhase.Update();
			
			if (gameOver)
			{
				GameOver();
			} else
			if (turnFinished)
			{
				NextTurn();
			}
		}
		
		public function StartGame():void
		{
			gameOver = CheckGameOverCondition();
			turnFinished = !gameOver;
		}
		
		private function CheckGameOverCondition():Boolean
		{
			return !(players.length > 1);
			
		}
		
		public function GameOver():void
		{
			//show stats or something and say bye bye
		}
		
		public function AddBuilding(building:Building):void
		{
			building.gameManager = this;
			buildings.push(building);
			world.add(building);
		}
		
		public function RemoveBuilding(building:Building):void
		{
			building.gameManager = null;
			var idx:int = buildings.indexOf(building);
			buildings.splice(idx, 1);
			world.remove(building);
		}
		
		public function NextTurn():void
		{
			//StartTurnPhase = announce start turn, change current player gui
			//AutoActionPhase - iterate through all player buildings and do autoaction
			//SelectBuildingPhase - let player select current active building among his controllable buildings
			//ControlBuildingPhase - make active current building
			//FinishTurnPhase - announce finish turn, clean mess
			
			
			turnFinished = false;
			turnPhases = [
				new StartTurnPhase(),
				new AutoActionPhase(),
				new SelectBuildingPhase(),
				new ControlBuildingPhase(),
				new FinishTurnPhase(),
			];
			currentPhaseIdx = 0;
			currentPhase = null;
			
			NextPhase();
			
		}
		
		public function NextPhase():void
		{
			if (currentPhase)
			{
				currentPhase.Finish();
				currentPhaseIdx++;
				if (currentPhaseIdx >= turnPhases.length)
				{
					currentPhaseIdx = 0;
					turnFinished = true;
					currentPhase = null;
					return;
				}
			}
			currentPhase = turnPhases[currentPhaseIdx];
			currentPhase.papa = this;
			currentPhase.Start();
			world.gui.ShowCurrentPhase(currentPhase);
		}
		
	}

}